﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices.ComTypes;
using Silverlight.Hosting;
using System.Drawing;
using Microsoft.Win32.SafeHandles;
using System.IO;

#if WPF
namespace Silverlight.Hosting.Wpf.Interop
#else
namespace Silverlight.Hosting.Interop
#endif
{
    internal enum OLEMISC
    {
        INVISIBLEATRUNTIME = 0x00000400,
        ALWAYSRUN = 0x00000800,
        ACTSLIKEBUTTON = 0x00001000,
        ACTSLIKELABEL = 0x00002000,
        NOUIACTIVATE = 0x00004000,
        ALIGNABLE = 0x00008000,
        SIMPLEFRAME = 0x00010000,
        SETCLIENTSITEFIRST = 0x00020000,
        IMEMODE = 0x00040000
    }

    internal static class HRESULT
    {
        internal const int NOERROR = 0;
        internal const int S_OK = 0;
        internal const int S_FALSE = 1;
        internal const int E_PENDING = unchecked((int)0x8000000A);
        internal const int E_HANDLE = unchecked((int)0x80070006);
        internal const int E_NOTIMPL = unchecked((int)0x80004001);
        internal const int E_NOINTERFACE = unchecked((int)0x80004002);
        internal const int E_POINTER = unchecked((int)0x80004003);
        internal const int E_ABORT = unchecked((int)0x80004004);
        internal const int E_FAIL = unchecked((int)0x80004005);
        internal const int E_OUTOFMEMORY = unchecked((int)0x8007000E);
        internal const int E_ACCESSDENIED = unchecked((int)0x80070005);
        internal const int E_UNEXPECTED = unchecked((int)0x8000FFFF);
        internal const int E_FLAGS = unchecked((int)0x1000);
        internal const int E_INVALIDARG = unchecked((int)0x80070057);
    }

    internal enum WindowMessages
    {
        WM_ACTIVATE = 0x0006,
        WM_ACTIVATEAPP = 0x001C,
        WM_AFXFIRST = 0x0360,
        WM_AFXLAST = 0x037F,
        WM_APP = 0x8000,
        WM_ASKCBFORMATNAME = 0x030C,
        WM_CANCELJOURNAL = 0x004B,
        WM_CANCELMODE = 0x001F,
        WM_CAPTURECHANGED = 0x0215,
        WM_CHANGECBCHAIN = 0x030D,
        WM_CHANGEUISTATE = 0x0127,
        WM_CHAR = 0x0102,
        WM_CHARTOITEM = 0x002F,
        WM_CHILDACTIVATE = 0x0022,
        WM_CLEAR = 0x0303,
        WM_CLOSE = 0x0010,
        WM_COMMAND = 0x0111,
        WM_COMPACTING = 0x0041,
        WM_COMPAREITEM = 0x0039,
        WM_CONTEXTMENU = 0x007B,
        WM_COPY = 0x0301,
        WM_COPYDATA = 0x004A,
        WM_CREATE = 0x0001,
        WM_CTLCOLORBTN = 0x0135,
        WM_CTLCOLORDLG = 0x0136,
        WM_CTLCOLOREDIT = 0x0133,
        WM_CTLCOLORLISTBOX = 0x0134,
        WM_CTLCOLORMSGBOX = 0x0132,
        WM_CTLCOLORSCROLLBAR = 0x0137,
        WM_CTLCOLORSTATIC = 0x0138,
        WM_CUT = 0x0300,
        WM_DEADCHAR = 0x0103,
        WM_DELETEITEM = 0x002D,
        WM_DESTROY = 0x0002,
        WM_DESTROYCLIPBOARD = 0x0307,
        WM_DEVICECHANGE = 0x0219,
        WM_DEVMODECHANGE = 0x001B,
        WM_DISPLAYCHANGE = 0x007E,
        WM_DRAWCLIPBOARD = 0x0308,
        WM_DRAWITEM = 0x002B,
        WM_DROPFILES = 0x0233,
        WM_ENABLE = 0x000A,
        WM_ENDSESSION = 0x0016,
        WM_ENTERIDLE = 0x0121,
        WM_ENTERMENULOOP = 0x0211,
        WM_ENTERSIZEMOVE = 0x0231,
        WM_ERASEBKGND = 0x0014,
        WM_EXITMENULOOP = 0x0212,
        WM_EXITSIZEMOVE = 0x0232,
        WM_FONTCHANGE = 0x001D,
        WM_GETDLGCODE = 0x0087,
        WM_GETFONT = 0x0031,
        WM_GETHOTKEY = 0x0033,
        WM_GETICON = 0x007F,
        WM_GETMINMAXINFO = 0x0024,
        WM_GETOBJECT = 0x003D,
        WM_GETTEXT = 0x000D,
        WM_GETTEXTLENGTH = 0x000E,
        WM_HANDHELDFIRST = 0x0358,
        WM_HANDHELDLAST = 0x035F,
        WM_HELP = 0x0053,
        WM_HOTKEY = 0x0312,
        WM_HSCROLL = 0x0114,
        WM_HSCROLLCLIPBOARD = 0x030E,
        WM_ICONERASEBKGND = 0x0027,
        WM_IME_CHAR = 0x0286,
        WM_IME_COMPOSITION = 0x010F,
        WM_IME_COMPOSITIONFULL = 0x0284,
        WM_IME_CONTROL = 0x0283,
        WM_IME_ENDCOMPOSITION = 0x010E,
        WM_IME_KEYDOWN = 0x0290,
        WM_IME_KEYLAST = 0x010F,
        WM_IME_KEYUP = 0x0291,
        WM_IME_NOTIFY = 0x0282,
        WM_IME_REQUEST = 0x0288,
        WM_IME_SELECT = 0x0285,
        WM_IME_SETCONTEXT = 0x0281,
        WM_IME_STARTCOMPOSITION = 0x010D,
        WM_INITDIALOG = 0x0110,
        WM_INITMENU = 0x0116,
        WM_INITMENUPOPUP = 0x0117,
        WM_INPUTLANGCHANGE = 0x0051,
        WM_INPUTLANGCHANGEREQUEST = 0x0050,
        WM_KEYDOWN = 0x0100,
        WM_KEYFIRST = 0x0100,
        WM_KEYLAST = 0x0108,
        WM_KEYUP = 0x0101,
        WM_KILLFOCUS = 0x0008,
        WM_LBUTTONDBLCLK = 0x0203,
        WM_LBUTTONDOWN = 0x0201,
        WM_LBUTTONUP = 0x0202,
        WM_MBUTTONDBLCLK = 0x0209,
        WM_MBUTTONDOWN = 0x0207,
        WM_MBUTTONUP = 0x0208,
        WM_MDIACTIVATE = 0x0222,
        WM_MDICASCADE = 0x0227,
        WM_MDICREATE = 0x0220,
        WM_MDIDESTROY = 0x0221,
        WM_MDIGETACTIVE = 0x0229,
        WM_MDIICONARRANGE = 0x0228,
        WM_MDIMAXIMIZE = 0x0225,
        WM_MDINEXT = 0x0224,
        WM_MDIREFRESHMENU = 0x0234,
        WM_MDIRESTORE = 0x0223,
        WM_MDISETMENU = 0x0230,
        WM_MDITILE = 0x0226,
        WM_MEASUREITEM = 0x002C,
        WM_MENUCHAR = 0x0120,
        WM_MENUCOMMAND = 0x0126,
        WM_MENUDRAG = 0x0123,
        WM_MENUGETOBJECT = 0x0124,
        WM_MENURBUTTONUP = 0x0122,
        WM_MENUSELECT = 0x011F,
        WM_MOUSEACTIVATE = 0x0021,
        WM_MOUSEFIRST = 0x0200,
        WM_MOUSEHOVER = 0x02A1,
        WM_MOUSELAST = 0x020D,
        WM_MOUSELEAVE = 0x02A3,
        WM_MOUSEMOVE = 0x0200,
        WM_MOUSEWHEEL = 0x020A,
        WM_MOUSEHWHEEL = 0x020E,
        WM_MOVE = 0x0003,
        WM_MOVING = 0x0216,
        WM_NCACTIVATE = 0x0086,
        WM_NCCALCSIZE = 0x0083,
        WM_NCCREATE = 0x0081,
        WM_NCDESTROY = 0x0082,
        WM_NCHITTEST = 0x0084,
        WM_NCLBUTTONDBLCLK = 0x00A3,
        WM_NCLBUTTONDOWN = 0x00A1,
        WM_NCLBUTTONUP = 0x00A2,
        WM_NCMBUTTONDBLCLK = 0x00A9,
        WM_NCMBUTTONDOWN = 0x00A7,
        WM_NCMBUTTONUP = 0x00A8,
        WM_NCMOUSEMOVE = 0x00A0,
        WM_NCPAINT = 0x0085,
        WM_NCRBUTTONDBLCLK = 0x00A6,
        WM_NCRBUTTONDOWN = 0x00A4,
        WM_NCRBUTTONUP = 0x00A5,
        WM_NEXTDLGCTL = 0x0028,
        WM_NEXTMENU = 0x0213,
        WM_NOTIFY = 0x004E,
        WM_NOTIFYFORMAT = 0x0055,
        WM_NULL = 0x0000,
        WM_PAINT = 0x000F,
        WM_PAINTCLIPBOARD = 0x0309,
        WM_PAINTICON = 0x0026,
        WM_PALETTECHANGED = 0x0311,
        WM_PALETTEISCHANGING = 0x0310,
        WM_PARENTNOTIFY = 0x0210,
        WM_PASTE = 0x0302,
        WM_PENWINFIRST = 0x0380,
        WM_PENWINLAST = 0x038F,
        WM_POWER = 0x0048,
        WM_POWERBROADCAST = 0x0218,
        WM_PRINT = 0x0317,
        WM_PRINTCLIENT = 0x0318,
        WM_QUERYDRAGICON = 0x0037,
        WM_QUERYENDSESSION = 0x0011,
        WM_QUERYNEWPALETTE = 0x030F,
        WM_QUERYOPEN = 0x0013,
        WM_QUEUESYNC = 0x0023,
        WM_QUIT = 0x0012,
        WM_RBUTTONDBLCLK = 0x0206,
        WM_RBUTTONDOWN = 0x0204,
        WM_RBUTTONUP = 0x0205,
        WM_RENDERALLFORMATS = 0x0306,
        WM_RENDERFORMAT = 0x0305,
        WM_SETCURSOR = 0x0020,
        WM_SETFOCUS = 0x0007,
        WM_SETFONT = 0x0030,
        WM_SETHOTKEY = 0x0032,
        WM_SETICON = 0x0080,
        WM_SETREDRAW = 0x000B,
        WM_SETTEXT = 0x000C,
        WM_SETTINGCHANGE = 0x001A,
        WM_SHOWWINDOW = 0x0018,
        WM_SIZE = 0x0005,
        WM_SIZECLIPBOARD = 0x030B,
        WM_SIZING = 0x0214,
        WM_SPOOLERSTATUS = 0x002A,
        WM_STYLECHANGED = 0x007D,
        WM_STYLECHANGING = 0x007C,
        WM_SYNCPAINT = 0x0088,
        WM_SYSCHAR = 0x0106,
        WM_SYSCOLORCHANGE = 0x0015,
        WM_SYSCOMMAND = 0x0112,
        WM_SYSDEADCHAR = 0x0107,
        WM_SYSKEYDOWN = 0x0104,
        WM_SYSKEYUP = 0x0105,
        WM_TCARD = 0x0052,
        WM_TIMECHANGE = 0x001E,
        WM_TIMER = 0x0113,
        WM_UNDO = 0x0304,
        WM_UNINITMENUPOPUP = 0x0125,
        WM_USER = 0x0400,
        WM_REFLECT = WM_USER + 0x1c00,
        WM_USERCHANGED = 0x0054,
        WM_VKEYTOITEM = 0x002E,
        WM_VSCROLL = 0x0115,
        WM_VSCROLLCLIPBOARD = 0x030A,
        WM_WINDOWPOSCHANGED = 0x0047,
        WM_WINDOWPOSCHANGING = 0x0046,
        WM_WININICHANGE = 0x001A,
        WM_XBUTTONDBLCLK = 0x020D,
        WM_XBUTTONDOWN = 0x020B,
        WM_XBUTTONUP = 0x020C,
    }

    [Flags]
    internal enum ModifierFlags
    {
        MK_LBUTTON = 0x0001,
        MK_RBUTTON = 0x0002,
        MK_SHIFT = 0x0004,
        MK_CONTROL = 0x0008,
        MK_MBUTTON = 0x0010,
        MK_XBUTTON1 = 0x0020,
        MK_XBUTTON2 = 0x0040,
    }

    [ComImport, Guid("6D5140C1-7436-11CE-8034-00AA006009FA"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
    internal interface IServiceProvider
    {
        [PreserveSig]
        int QueryService([In] ref Guid guidService, [In] ref Guid riid, out IntPtr ppvObject);
    }

    [ComImport, InterfaceType(ComInterfaceType.InterfaceIsIUnknown), Guid("0000011A-0000-0000-C000-000000000046")]
    internal interface IParseDisplayName
    {
        [PreserveSig]
        int ParseDisplayName([In, MarshalAs(UnmanagedType.Interface)] IBindCtx pbc,
                              [In, MarshalAs(UnmanagedType.LPWStr)] string pszDisplayName,
                              out uint pchEaten,
                              [MarshalAs(UnmanagedType.Interface)] out IMoniker ppmkOut);
    }

    [ComImport, InterfaceType(ComInterfaceType.InterfaceIsIUnknown), Guid("00000100-0000-0000-C000-000000000046")]
    internal interface IEnumUnknown
    {
        [PreserveSig]
        int Next([In] uint celt, [Out, MarshalAs(UnmanagedType.LPArray, ArraySubType = UnmanagedType.IUnknown)] object[] rgelt, out uint pceltFetched);
        [PreserveSig]
        int Skip([In] uint celt);
        [PreserveSig]
        int Reset();
        [PreserveSig]
        int Clone([MarshalAs(UnmanagedType.Interface)] out IEnumUnknown ppEnum);
    }

    [ComImport, Guid("0000011B-0000-0000-C000-000000000046"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
    internal interface IOleContainer : IParseDisplayName
    {
        [PreserveSig]
        new int ParseDisplayName([In, MarshalAs(UnmanagedType.Interface)] IBindCtx pbc, [In, MarshalAs(UnmanagedType.LPWStr)] string pszDisplayName, out uint pchEaten, [MarshalAs(UnmanagedType.Interface)] out IMoniker ppmkOut);
        [PreserveSig]
        int EnumObjects([In] uint grfFlags, [MarshalAs(UnmanagedType.Interface)] out IEnumUnknown ppEnum);
        [PreserveSig]
        int LockContainer([In] int fLock);
    }

    [ComImport, InterfaceType(ComInterfaceType.InterfaceIsIUnknown), Guid("00000118-0000-0000-C000-000000000046")]
    internal interface IOleClientSite
    {
        [PreserveSig]
        int SaveObject();
        [PreserveSig]
        int GetMoniker([In] uint dwAssign, [In] uint dwWhichMoniker, [MarshalAs(UnmanagedType.Interface)] out IMoniker ppmk);
        [PreserveSig]
        int GetContainer([MarshalAs(UnmanagedType.Interface)] out IOleContainer ppContainer);
        [PreserveSig]
        int ShowObject();
        [PreserveSig]
        int OnShowWindow([In] int fShow);
        [PreserveSig]
        int RequestNewObjectLayout();
    }

    [StructLayout(LayoutKind.Sequential)]
    internal struct MSG
    {
        public IntPtr hwnd;
        public int message;
        public IntPtr wParam;
        public IntPtr lParam;
        public int time;
        public POINT pt;

        public static MSG Create(IntPtr hwnd, int message, IntPtr wParam, IntPtr lParam)
        {
            return new MSG()
            {
                hwnd = hwnd,
                message = message,
                wParam = wParam,
                lParam = lParam
            };
        }
    }

    [StructLayout(LayoutKind.Sequential)]
    internal struct SIZE
    {
        public int cx;
        public int cy;
    }

    [StructLayout(LayoutKind.Sequential)]
    internal struct RECT
    {
        public int left;
        public int top;
        public int right;
        public int bottom;

        public static RECT FromRectangle(Rectangle rectangle)
        {
            RECT r = new RECT();
            r.left = rectangle.Left;
            r.top = rectangle.Top;
            r.right = rectangle.Right;
            r.bottom = rectangle.Bottom;
            return r;
        }
    }

    [StructLayout(LayoutKind.Sequential)]
    internal struct POINT
    {
        public int x;
        public int y;
    }

    [StructLayout(LayoutKind.Sequential, Pack = 2)]
    internal struct LOGPALETTE
    {
        public ushort palVersion;
        public ushort palNumEntries;
        public IntPtr palPalEntry;
    }

    [StructLayout(LayoutKind.Sequential)]
    internal struct OLEVERB
    {
        public int lVerb;
        [MarshalAs(UnmanagedType.LPWStr)]
        public string lpszVerbName;
        public uint fuFlags;
        public uint grfAttribs;
    }

    [ComImport, Guid("00000104-0000-0000-C000-000000000046"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
    internal interface IEnumOLEVERB
    {
        int Next([In] uint celt, [Out, MarshalAs(UnmanagedType.LPArray, SizeParamIndex = 0)] OLEVERB[] rgelt, out uint pceltFetched);
        int Skip([In] uint celt);
        int Reset();
        void Clone([MarshalAs(UnmanagedType.Interface)] out IEnumOLEVERB ppEnum);
    }

    [ComImport, ComConversionLoss, Guid("00000112-0000-0000-C000-000000000046"), InterfaceType((short)1)]
    internal interface IOleObject
    {
        [PreserveSig]
        int SetClientSite([In, MarshalAs(UnmanagedType.Interface)] IOleClientSite pClientSite);
        [PreserveSig]
        int GetClientSite([MarshalAs(UnmanagedType.Interface)] out IOleClientSite ppClientSite);
        [PreserveSig]
        int SetHostNames([In, MarshalAs(UnmanagedType.LPWStr)] string szContainerApp, [In, MarshalAs(UnmanagedType.LPWStr)] string szContainerObj);
        [PreserveSig]
        int Close([In] uint dwSaveOption);
        [PreserveSig]
        int SetMoniker([In] uint dwWhichMoniker, [In, MarshalAs(UnmanagedType.Interface)] IMoniker pmk);
        [PreserveSig]
        int GetMoniker([In] uint dwAssign, [In] uint dwWhichMoniker, [MarshalAs(UnmanagedType.Interface)] out IMoniker ppmk);
        [PreserveSig]
        int InitFromData([In, MarshalAs(UnmanagedType.Interface)] IDataObject pDataObject, [In] int fCreation, [In] uint dwReserved);
        [PreserveSig]
        int GetClipboardData([In] uint dwReserved, [MarshalAs(UnmanagedType.Interface)] out IDataObject ppDataObject);
        [PreserveSig]
        int DoVerb([In] int iVerb, [In, MarshalAs(UnmanagedType.Struct)]ref MSG lpmsg, [In, MarshalAs(UnmanagedType.Interface)] IOleClientSite pActiveSite, [In] int lindex, [In] IntPtr hWndParent, [In, MarshalAs(UnmanagedType.Struct)]ref RECT lprcPosRect);
        [PreserveSig]
        int EnumVerbs([MarshalAs(UnmanagedType.Interface)] out IEnumOLEVERB ppEnumOleVerb);
        [PreserveSig]
        int Update();
        [PreserveSig]
        int IsUpToDate();
        [PreserveSig]
        int GetUserClassID(out Guid pClsid);
        [PreserveSig]
        int GetUserType([In] uint dwFormOfType, [In] IntPtr pszUserType);
        [PreserveSig]
        int SetExtent([In] uint dwDrawAspect, [In, MarshalAs(UnmanagedType.LPArray)] SIZE[] pSizel);
        [PreserveSig]
        int GetExtent([In] uint dwDrawAspect, [MarshalAs(UnmanagedType.Struct)] ref SIZE pSizel);
        [PreserveSig]
        int Advise([In, MarshalAs(UnmanagedType.Interface)] IAdviseSink pAdvSink, out uint pdwConnection);
        [PreserveSig]
        int Unadvise([In] uint dwConnection);
        [PreserveSig]
        int EnumAdvise([MarshalAs(UnmanagedType.Interface)] out IEnumSTATDATA ppenumAdvise);
        [PreserveSig]
        int GetMiscStatus([In] uint dwAspect, out uint pdwStatus);
        [PreserveSig]
        int SetColorScheme([In, MarshalAs(UnmanagedType.LPArray)] LOGPALETTE[] pLogpal);
    }

    [ComImport, InterfaceType(ComInterfaceType.InterfaceIsIUnknown), Guid("0000010C-0000-0000-C000-000000000046")]
    internal interface IPersist
    {
        [PreserveSig]
        int GetClassID(out Guid pClassID);
    }

    [ComImport]
    [Guid("55272A00-42CB-11CE-8135-00AA004BB851")]
    [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
    internal interface IPropertyBag
    {
        [PreserveSig]
        int Read(
          [In, MarshalAs(UnmanagedType.LPWStr)] string propName,
          [Out, MarshalAs(UnmanagedType.Struct)] out object value,
          [In] IErrorLog errorLog
        );

        [PreserveSig]
        int Write(
          [In, MarshalAs(UnmanagedType.LPWStr)] string propName,
          [In, MarshalAs(UnmanagedType.Struct)] ref object value
        );
    }


    [ComImport, Guid("3127CA40-446E-11CE-8135-00AA004BB851"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
    internal interface IErrorLog
    {
        [PreserveSig]
        int AddError([In, MarshalAs(UnmanagedType.LPWStr)] string pszPropName,
                      [In, MarshalAs(UnmanagedType.Struct)] System.Runtime.InteropServices.ComTypes.EXCEPINFO pExcepInfo);
    }

    [ComImport, Guid("37D84F60-42CB-11CE-8135-00AA004BB851"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
    internal interface IPersistPropertyBag : IPersist
    {
        [PreserveSig]
        new int GetClassID(out Guid pClassID);
        [PreserveSig]
        int InitNew();
        [PreserveSig]
        int Load([In, MarshalAs(UnmanagedType.Interface)] IPropertyBag pPropBag, [In, MarshalAs(UnmanagedType.Interface)] IErrorLog pErrorLog);
        [PreserveSig]
        int Save([In, MarshalAs(UnmanagedType.Interface)] IPropertyBag pPropBag, [In] int fClearDirty, [In] int fSaveAllProperties);
    }

    [ComVisible(true), ComImport, InterfaceType(ComInterfaceType.InterfaceIsIUnknown), Guid("00000117-0000-0000-C000-000000000046")]
    internal interface IOleInPlaceActiveObject
    {
        [PreserveSig]
        int GetWindow(out IntPtr hwnd);
        void ContextSensitiveHelp(int fEnterMode);
        [PreserveSig]
        int TranslateAccelerator([In] ref MSG lpmsg);
        void OnFrameWindowActivate(bool fActivate);
        void OnDocWindowActivate(int fActivate);
        void ResizeBorder([In] RECT prcBorder, [In] IOleInPlaceUIWindow pUIWindow, bool fFrameWindow);
        void EnableModeless(int fEnable);
    }

    [ComVisible(true), ComImport(), Guid("00000115-0000-0000-C000-000000000046"), InterfaceTypeAttribute(ComInterfaceType.InterfaceIsIUnknown)]
    internal interface IOleInPlaceUIWindow
    {
        //IOleWindow
        [return: MarshalAs(UnmanagedType.I4)]
        [PreserveSig]
        int GetWindow([In, Out] ref IntPtr phwnd);

        [return: MarshalAs(UnmanagedType.I4)]
        [PreserveSig]
        int ContextSensitiveHelp([In, MarshalAs(UnmanagedType.Bool)] bool fEnterMode);

        //IOleInPlaceUIWindow
        [return: MarshalAs(UnmanagedType.I4)]
        [PreserveSig]
        int GetBorder([In, Out, MarshalAs(UnmanagedType.Struct)] ref RECT lprectBorder);

        [return: MarshalAs(UnmanagedType.I4)]
        [PreserveSig]
        int RequestBorderSpace([In, MarshalAs(UnmanagedType.Struct)] ref RECT pborderwidths);

        [return: MarshalAs(UnmanagedType.I4)]
        [PreserveSig]
        int SetBorderSpace([In, MarshalAs(UnmanagedType.Struct)] ref RECT pborderwidths);

        [return: MarshalAs(UnmanagedType.I4)]
        [PreserveSig]
        int SetActiveObject(
            [In, MarshalAs(UnmanagedType.Interface)]
                ref IOleInPlaceActiveObject pActiveObject,
            [In, MarshalAs(UnmanagedType.LPWStr)]
                string pszObjName);
    }

    internal enum OLEDOVERB : int
    {
        DISCARDUNDOSTATE = -6,
        HIDE = -3,
        INPLACEACTIVATE = -5,
        NOSAVE = 1,
        OPEN = -2,
        PRIMARY = 0,
        PROPERTIES = -7,
        SHOW = -1,
        UIACTIVATE = -4
    }

    [ComVisible(true), ComImport(), Guid("00000113-0000-0000-C000-000000000046"), InterfaceTypeAttribute(ComInterfaceType.InterfaceIsIUnknown)]
    internal interface IOleInPlaceObject
    {
        [PreserveSig]
        int GetWindow(out IntPtr phwnd);
        [PreserveSig]
        int ContextSensitiveHelp([In] int fEnterMode);

        //IOleInPlaceObject
        void InPlaceDeactivate();

        [return: MarshalAs(UnmanagedType.I4)]
        [PreserveSig]
        int UIDeactivate();

        [return: MarshalAs(UnmanagedType.I4)]
        [PreserveSig]
        int SetObjectRects(
           [In, MarshalAs(UnmanagedType.Struct)] ref RECT lprcPosRect,
           [In, MarshalAs(UnmanagedType.Struct)] ref RECT lprcClipRect);

        void ReactivateAndUndo();
    }

    [ComImport, Guid("922EADA0-3424-11CF-B670-00AA004CD6D8"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
    internal interface IOleInPlaceSiteWindowless// : IOleInPlaceSiteEx
    {
        [PreserveSig]
        int GetWindow(out IntPtr phwnd);
        [PreserveSig]
        int ContextSensitiveHelp([In] int fEnterMode);
        [PreserveSig]
        int CanInPlaceActivate();
        [PreserveSig]
        int OnInPlaceActivate();
        [PreserveSig]
        int OnUIActivate();
        [PreserveSig]
        int GetWindowContext([MarshalAs(UnmanagedType.Interface)] out IOleInPlaceFrame ppFrame,
                                 [MarshalAs(UnmanagedType.Interface)] out IOleInPlaceUIWindow ppDoc,
                                 [Out, MarshalAs(UnmanagedType.LPArray)] RECT[] lprcPosRect,
                                 [Out, MarshalAs(UnmanagedType.LPArray)] RECT[] lprcClipRect,
                                 [In, Out, MarshalAs(UnmanagedType.LPArray)] OLEINPLACEFRAMEINFO[] lpFrameInfo);
        [PreserveSig]
        int Scroll([In] SIZE scrollExtant);
        [PreserveSig]
        int OnUIDeactivate([In] int fUndoable);
        [PreserveSig]
        int OnInPlaceDeactivate();
        [PreserveSig]
        int DiscardUndoState();
        [PreserveSig]
        int DeactivateAndUndo();
        [PreserveSig]
        int OnPosRectChange([In, MarshalAs(UnmanagedType.LPArray)] RECT[] lprcPosRect);
        [PreserveSig]
        int OnInPlaceActivateEx(out int pfNoRedraw, [In] uint dwFlags);
        [PreserveSig]
        int OnInPlaceDeactivateEx([In] int fNoRedraw);
        [PreserveSig]
        int RequestUIActivate();
        [PreserveSig]
        int CanWindowlessActivate();
        [PreserveSig]
        int GetCapture();
        [PreserveSig]
        int SetCapture([In] int fCapture);
        [PreserveSig]
        int GetFocus();
        [PreserveSig]
        int SetFocus([In] int fFocus);
        [PreserveSig]
        int GetDC([In, MarshalAs(UnmanagedType.LPArray)] RECT[] pRect, [In] uint grfFlags, out IntPtr phDC);
        [PreserveSig]
        int ReleaseDC([In] IntPtr hDC);
        [PreserveSig]
        int InvalidateRect([In, MarshalAs(UnmanagedType.LPArray)] RECT[] pRect, [In] int fErase);
        [PreserveSig]
        int InvalidateRgn([In] IntPtr hRGN, [In] int fErase);
        [PreserveSig]
        int ScrollRect([In] int dx, [In] int dy, [In, MarshalAs(UnmanagedType.LPArray)] RECT[] pRectScroll, [In, MarshalAs(UnmanagedType.LPArray)] RECT[] pRectClip);
        [PreserveSig]
        int AdjustRect([In, Out, MarshalAs(UnmanagedType.LPArray)] RECT[] prc);
        [PreserveSig]
        int OnDefWindowMessage([In] uint MSG, [In] IntPtr wParam, [In] IntPtr lParam, out int plResult);
    }

    [StructLayout(LayoutKind.Sequential)]
    internal struct OLEINPLACEFRAMEINFO
    {
        public uint cb;
        public int fMDIApp;
        public IntPtr hwndFrame;
        public IntPtr haccel;
        public uint cAccelEntries;
    }

    [ComImport, Guid("1C2056CC-5EF4-101B-8BC8-00AA003E3B29"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
    internal interface IOleInPlaceObjectWindowless : IOleInPlaceObject
    {
        [PreserveSig]
        new int GetWindow(out IntPtr phwnd);
        [PreserveSig]
        new int ContextSensitiveHelp([In] int fEnterMode);
        [PreserveSig]
        new int InPlaceDeactivate();
        [PreserveSig]
        new int UIDeactivate();
        [PreserveSig]
        int SetObjectRects([In, MarshalAs(UnmanagedType.LPArray)] RECT[] lprcPosRect,
                           [In, MarshalAs(UnmanagedType.LPArray)] RECT[] lprcClipRect);
        [PreserveSig]
        new int ReactivateAndUndo();
        [PreserveSig]
        int OnWindowMessage([In] uint MSG, [In] IntPtr wParam, [In] IntPtr lParam, out int plResult);
        [PreserveSig]
        int GetDropTarget([MarshalAs(UnmanagedType.Interface)] out IDropTarget ppDropTarget);
    }

    [ComImport, InterfaceType(ComInterfaceType.InterfaceIsIUnknown), Guid("00000122-0000-0000-C000-000000000046")]
    internal interface IDropTarget
    {
        [PreserveSig]
        int DragEnter([In, MarshalAs(UnmanagedType.Interface)] IDataObject pDataObj, [In] uint grfKeyState, [In] POINT pt, [In, Out] ref uint pdwEffect);
        [PreserveSig]
        int DragOver([In] uint grfKeyState, [In] POINT pt, [In, Out] ref uint pdwEffect);
        [PreserveSig]
        int DragLeave();
        [PreserveSig]
        int Drop([In, MarshalAs(UnmanagedType.Interface)] IDataObject pDataObj, [In] uint grfKeyState, [In] POINT pt, [In, Out] ref uint pdwEffect);
    }

    [StructLayout(LayoutKind.Sequential)]
    internal struct OLEMENUGROUPWIDTHS
    {
        [MarshalAs(UnmanagedType.ByValArray, SizeConst = 6)]
        public int[] width;
    }

    [ComImport, InterfaceType(ComInterfaceType.InterfaceIsIUnknown), Guid("00000116-0000-0000-C000-000000000046")]
    internal interface IOleInPlaceFrame : IOleInPlaceUIWindow
    {
        //IOleWindow
        [return: MarshalAs(UnmanagedType.I4)]
        [PreserveSig]
        new int GetWindow([In, Out] ref IntPtr phwnd);

        [return: MarshalAs(UnmanagedType.I4)]
        [PreserveSig]
        new int ContextSensitiveHelp([In, MarshalAs(UnmanagedType.Bool)] bool fEnterMode);

        //IOleInPlaceUIWindow
        [return: MarshalAs(UnmanagedType.I4)]
        [PreserveSig]
        new int GetBorder([In, Out, MarshalAs(UnmanagedType.Struct)] ref RECT lprectBorder);

        [return: MarshalAs(UnmanagedType.I4)]
        [PreserveSig]
        new int RequestBorderSpace([In, MarshalAs(UnmanagedType.Struct)] ref RECT pborderwidths);

        [return: MarshalAs(UnmanagedType.I4)]
        [PreserveSig]
        new int SetBorderSpace([In, MarshalAs(UnmanagedType.Struct)] ref RECT pborderwidths);

        [return: MarshalAs(UnmanagedType.I4)]
        [PreserveSig]
        new int SetActiveObject(
            [In, MarshalAs(UnmanagedType.Interface)]
                ref IOleInPlaceActiveObject pActiveObject,
            [In, MarshalAs(UnmanagedType.LPWStr)]
                string pszObjName);
        
        [PreserveSig]
        int InsertMenus([In] IntPtr hmenuShared, [In, Out, MarshalAs(UnmanagedType.LPArray)] OLEMENUGROUPWIDTHS[] lpMenuWidths);
        [PreserveSig]
        int SetMenu([In] IntPtr hmenuShared, [In] IntPtr HOLEMENU, [In] IntPtr hwndActiveObject);
        [PreserveSig]
        int RemoveMenus([In] IntPtr hmenuShared);
        [PreserveSig]
        int SetStatusText([In, MarshalAs(UnmanagedType.LPWStr)] string pszStatusText);
        [PreserveSig]
        int EnableModeless([In] int fEnable);
        [PreserveSig]
        int TranslateAccelerator([In, MarshalAs(UnmanagedType.LPArray)] MSG[] lpmsg, [In] ushort wID);
    }

}
